//-----------------------------------------------------------------------
//<copyright file="PhieuXuatBLL.cs
//Code gen by tool.
//</copyright>
//-----------------------------------------------------------------------
using System;
using PMS.Exceptions;
using System.Collections.Generic;
using PMS.DAL;
using PMS.Entity;
using System.Transactions;

namespace PMS.BLL
{
	public class PhieuXuatBO : BaseBO
	{
		// Interface Instant Object
		private readonly IPhieuXuatDAO phieuxuatInterfaceObject = null;
        private readonly ChiTietPhieuXuatBO ChiTietPhieuXuatInterfaceObject = null;
        private readonly LuuKhoBO LuuKhoInterfaceObject = null;
        private readonly LuuKhoBO LuuKhoService = null; //add new line

		// Constructor
		public PhieuXuatBO()
		{
			phieuxuatInterfaceObject = new DAL.ServiceFactory<IPhieuXuatDAO>().CurrentDAL;
            ChiTietPhieuXuatInterfaceObject = new ChiTietPhieuXuatBO();
            LuuKhoInterfaceObject = new LuuKhoBO();
            LuuKhoService = new LuuKhoBO();
		}

		#region PhieuXuat

		/// <summary>
		///
		/// </summary>
		/// <param name="phieuxuat"></param>
		public PhieuXuat CreatePhieuXuat(PhieuXuat phieuxuat, ChiTietPhieuXuats DetailList)
		{
            string newstring = phieuxuat.SoPX;
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    phieuxuat = phieuxuatInterfaceObject.CreatePhieuXuat(phieuxuat);
                    foreach (ChiTietPhieuXuat item in DetailList)
                    {
                        item.SoPX = phieuxuat.SoPX;
                        ChiTietPhieuXuatInterfaceObject.CreateChiTietPhieuXuat(item);
                        LuuKhoInterfaceObject.UpdateLuuKhoXuatKho(item.MaLuuKho, item.SoLuong);
                    }
                }
                catch (Exception ex)
                {
                    phieuxuat.SoPX = newstring;
                    throw BusinessException.GetException("Business Access CreatePhieuNhap Exception", ex);
                }
                scope.Complete();
            }
            return phieuxuat;
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="phieuxuat"></param>
        public void UpdatePhieuXuat(PhieuXuat phieuxuat, ChiTietPhieuXuats DetailList)
		{
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    phieuxuatInterfaceObject.UpdatePhieuXuat(phieuxuat);
                    //ChiTietPhieuXuatInterfaceObject.DeleteChiTietPhieuXuatBySoPX(phieuxuat.SoPX);
                    foreach (ChiTietPhieuXuat item in DetailList)
                    {
                        //item.SoPX = phieuxuat.SoPX;
                        //ChiTietPhieuXuatInterfaceObject.CreateChiTietPhieuXuat(item);
                        ChiTietPhieuXuat xuatCu = ChiTietPhieuXuatInterfaceObject.GetChiTietPhieuXuatByID(item.MaCTPX);
                        ChiTietPhieuXuatInterfaceObject.UpdateChiTietPhieuXuat(item);
                        LuuKho luuKho = LuuKhoService.GetLuuKhoByID(item.MaLuuKho);
                        luuKho.TonKho = luuKho.TonKho + (xuatCu.SoLuong - item.SoLuong);
                        luuKho.TongXuat = luuKho.TongXuat + (item.SoLuong - xuatCu.SoLuong);
                        LuuKhoService.UpdateLuuKho(luuKho);
                    }
                }
                catch (Exception ex)
                {
                    throw BusinessException.GetException("Business Access UpdatePhieuNhapException", ex);
                }
                scope.Complete();
            }
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="phieuxuat"></param>
		public void DeletePhieuXuat(PhieuXuat phieuxuat)
		{
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    ChiTietPhieuXuatInterfaceObject.DeleteChiTietPhieuXuatBySoPX(phieuxuat.SoPX);
                    phieuxuatInterfaceObject.DeletePhieuXuat(phieuxuat);
                }
                catch (Exception ex)
                {
                    throw BusinessException.GetException("Business Access DeletePhieuXuatException", ex);
                }
                scope.Complete();
            }
		}

		/// <summary>
		///
		/// </summary>
		public PhieuXuats GetAllPhieuXuat()
		{
			try
			{
				return phieuxuatInterfaceObject.GetAllPhieuXuat();
			}
			catch (Exception ex)
			{
				throw BusinessException.GetException("Business Access GetAllPhieuXuat Exception", ex);
			}
		}

        /// <summary>
        ///
        /// </summary>
        public PhieuXuats GetAllPhieuXuatForPhieuGiao(bool isHavePhieuGiao)
        {
            try
            {
                return phieuxuatInterfaceObject.GetAllPhieuXuatForPhieuGiao(isHavePhieuGiao);
            }
            catch (Exception ex)
            {
                throw BusinessException.GetException("Business Access GetAllPhieuXuat Exception", ex);
            }
        }

		/// <summary>
		///
		/// </summary>
		/// <param name="soPX"></param>
		public PhieuXuat GetPhieuXuatByID(string soPX)
		{
			try
			{
				return phieuxuatInterfaceObject.GetPhieuXuatByID(soPX);
			}
			catch (Exception ex)
			{
				throw BusinessException.GetException("Business Access GetPhieuXuatBy ID Exception", ex);
			}
		}

        public PhieuXuats GetPhieuXuatByDate(DateTime? tungay, DateTime? denngay)
        {
            try
            {
                return phieuxuatInterfaceObject.GetPhieuXuatByDate(tungay, denngay);
            }
            catch (Exception ex)
            {
                throw BusinessException.GetException("Business Access GetPhieuXuatByDate Exception", ex);
            }
        }

		#endregion
	}
}
